
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.util.Arrays;

public class RxEngine extends Thread
{
	private DatagramSocket socket;

	
	public RxEngine(DatagramSocket ds)
	{
		socket = ds;	
	}

	public void receive() throws InstantiationException, IOException
	{
		byte [] buffer = new byte [1526];
		DatagramPacket receivedPacket = new DatagramPacket(buffer, buffer.length);
		
		EthernetFrame receivedMessage = null;
		
		while(true)
		{
				socket.receive(receivedPacket);
				
				
				//Extract data from receivedPacket and truncate excess padding
				byte [] data = receivedPacket.getData();
		        byte [] frame = new byte[receivedPacket.getLength()];
		        System.arraycopy(data, 0, frame, 0, frame.length);
		        
				receivedMessage = new EthernetFrame(frame);
				
				//Ethernet information for testing
				System.out.println("Ethernet Frame to be recieved:\n" + receivedMessage.toString());
		        System.out.println(new String(receivedMessage.getFramePayload()));
		        
		        
		        
		}
		
		 
	}
	
}